package com.kizuki.controller;

import com.kizuki.pojo.User;
import com.kizuki.repository.ESUserRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@Slf4j
@RestController
public class MyESController {

    @Autowired
    private ESUserRepository repository;

    // 删除
    @GetMapping("/es_delete/{id}")
    public String deleteEs(@PathVariable String id){
        repository.deleteById(id);
        log.info("删除es成功, id:{}", id);
        return "true";
    }

    // 增加/修改
    @GetMapping("/es_add")
    public String addEs(){
        User user = new User(100, "李达康/es_add");
        User save = repository.save(user);
        log.info("插入es成功：{}", save);
        return save.toString();
    }

    // 查询
    @GetMapping("/es_search/{id}")
    public String searchEs(@PathVariable String id){
        Optional<User> user = repository.findById(id);
        if (user.isPresent()) {
            User user1 = user.get();
            log.info("查询es成功, id:{}, user:{}", id, user1);
            return user1.toString();
        } else {
            log.info("查询es失败, id:{}", id);
            return "false";
        }
    }

    // 查询
    @GetMapping("/es_search")
    public String searchEs(){
        Iterable<User> all = repository.findAll();
        all.forEach(u -> log.info("查询es成功, user:{}", u));
        return "true";
    }

}
